package com.ly.mpayment.persist.service.ele;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.ly.mpayment.core.util.DateUtils;
import com.ly.mpayment.core.util.MapUtil;
import com.ly.mpayment.persist.entity.ele.EleRecord;
import com.ly.mpayment.persist.hibernate.Page;
import com.ly.mpayment.persist.service.AbstractService;


@Service
public class EleRecordServiceImpl extends AbstractService<EleRecord, Serializable> implements IEleRecordService {

	@Override
	public Page<EleRecord> findPageRecord(Map<String, Object> parameters, int start, int pageSize) {
		String hql = "select e from EleRecord e left join fetch e.electronicAccount ea where 1 = 1 #[and e.phoneNum = :phoneNum]#  #[and e.rechargeState = :rechargeState]# "
				+ "  #[and e.flow = :flow]#  #[and e.amount = :amount]# #[and ea.accountName = :accountName]# ";
		
		if(parameters.containsKey(":startTime")){//%Y-%m-%d %T   %Y-%m-%d %H:%i:%S
			hql += " and DATE_FORMAT(e.startTime,'%Y-%m-%d %T') >= '".concat(parameters.get(":startTime").toString()).concat("'");
			parameters.remove(":startTime");
		}
		if(parameters.containsKey(":endTime")){
			hql += " and DATE_FORMAT(e.endTime,'%Y-%m-%d %T') <= '".concat(parameters.get(":endTime").toString()).concat("'");
			parameters.remove(":endTime");
		}
		hql += " order by e.startTime desc";
		return this.findPageByNamed(hql, parameters, start, pageSize);
	}

	@Override
	public EleRecord findById(String id) {
		return this.findUnique("select e from EleRecord e left join fetch e.upOrder where e.recordId = ? ", id);
	}

	@Override
	public boolean saveEleRecord(EleRecord record) {
		if(record != null){
			this.save(record);
			this.getSession().flush();
			return true;
		}
		return false;
	}

	@Override
	public boolean updateEleRecord(EleRecord record) {
		if(record != null){
			this.updateEntity(record);
			return true;
		}
		return false;
	}

	@Override
	public EleRecord findPhoneDay(String phoneNum) {
//		List<EleRecord> eles = this.find("select e from EleRecord e where e.phoneNum = ? and DATE_FORMAT(e.startTime,'%Y-%m-%d') = ? order by e.startTime desc",  phoneNum,DateUtils.toShortDate(DateUtils.getNowDate()));
//		if(eles != null && !eles.isEmpty()){
//			return eles.get(0);
//		}
//		return null;
		return this.findUnique("select e from EleRecord e where e.phoneNum = ? and DATE_FORMAT(e.startTime,'%Y-%m-%d') = ? order by e.startTime desc", phoneNum,DateUtils.toShortDate(DateUtils.getNowDate()));
	}

	@Override
	public List<EleRecord> findAllNotCharge(int pageSize) {
		return this.findPageDataByNamed("select e from EleRecord e where e.rechargeState = 5 order by e.startTime", MapUtil.getMap(), 0, pageSize).getList();
	}

	@Override
	public List<EleRecord> findAllCharge(int pageSize) {
		return this.findPageDataByNamed("select e from EleRecord e where e.rechargeState = 1 order by e.startTime", MapUtil.getMap(), 0, pageSize).getList();
	}

	@Override
	public EleRecord findByIdAccount(String id) {
		return this.findUnique("select e from EleRecord e left join fetch e.electronicAccount where e.recordId = ? ", id);
	}

	@Override
	public List<EleRecord> queryStatus() {
		return this.find("select e from EleRecord e  where e.rechargeState = 1");
	}


}
